﻿using Common.Library;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.ComponentModel;
using OnlineVideos.Data;
using OnlineVideos.Entities;
using System.Threading.Tasks;


namespace Social_Celebrities
{
    public static class CreateCustomImage
    {
       static string fullpath = string.Empty;
    
       static Page Navigationpurpose = default(Page);
     
        public static void createFrontImage(Page currentpage)
        {
            try
            {
                    Grid grid = new Grid();
                    BitmapImage Bitmap = new BitmapImage();
                    Bitmap.CreateOptions = BitmapCreateOptions.None;
                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile("personImage.jpg", FileMode.Open, FileAccess.Read))
                        {
                            Bitmap.SetSource(local);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }
                    Image ProductImage = new Image();
                    ProductImage.Source = Bitmap;
                    grid.Children.Add(ProductImage);
                    WriteableBitmap wbmp = new WriteableBitmap(grid, null);
                    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!myIsolatedStorage.DirectoryExists(ResourceHelper.ProjectName))
                            myIsolatedStorage.CreateDirectory(ResourceHelper.ProjectName);
                        if (!myIsolatedStorage.DirectoryExists(ResourceHelper.ProjectName + "/" + "Images/" + "PersonImages"))
                        myIsolatedStorage.CreateDirectory(ResourceHelper.ProjectName + "/" + "Images/" + "PersonImages");
                        string personid = string.Empty;
                        //OnlineVideoDataContext context = new OnlineVideoDataContext(Constants.DatabaseConnectionString);
                        if (Constants.editcelebrity == true)
                        {
                            personid = Constants.personid.ToString();
                        }
                        else
                        {
                            if (Task.Run(async () => await Constants.connection.Table<CastProfile>().OrderByDescending(i => i.PersonID).FirstOrDefaultAsync()).Result != null)
                                personid = (Task.Run(async () => await Constants.connection.Table<CastProfile>().OrderByDescending(i => i.PersonID).FirstOrDefaultAsync()).Result.PersonID + 1).ToString();
                            else
                                personid = "1";
                        }
                        using (IsolatedStorageFileStream local = myIsolatedStorage.OpenFile(ResourceHelper.ProjectName + "/" + "Images" + "/" + "PersonImages" + "/" + personid + ".jpg", System.IO.FileMode.Create))
                        {
                            wbmp.SaveJpeg(local, 120, 110, 0, 100);
                            local.Close();
                            local.Dispose();
                            myIsolatedStorage.Dispose();
                        }
                    }
                    
                    currentpage.NavigationService.GoBack();
               
            }
            catch
            {
            }
            
        }

        
    }
}